const query = require('../models');

module.exports = {
  total(req, res) {
    const sql = `SELECT COUNT(*) total FROM students WHERE is_del=1`;

    query(sql, null)
      .then(function (result) {
        res.json({
          code: '200',
          msg: '操作成功',
          data: result[0]
        })
      })
      .catch(function (err) {
        res.json({
          code: '1002',
          msg: '操作失败',
          err
        })
      })
  },
  get(req, res) {
    let { current, pageSize } = req.query;
    current = Number(current);
    pageSize = Number(pageSize);
    let start = (current - 1) * pageSize;

    const sql = `
      SELECT s.student_id studentId,s.student_name studentName,c.class_name className 
      FROM students s 
      JOIN classes c 
      ON s.class_id=c.class_id
      WHERE s.is_del=1
      order by s.student_id desc
      limit ?,?`;

    query(sql, [start, pageSize])
      .then(function (result) {
        res.json({
          code: '200',
          msg: '查询成功',
          data: result
        });
      })
      .catch(function (err) {
        res.json({
          code: '1002',
          err
        });
      });
  },
  del: function (req, res) {
    const sql = `update students set is_del=0 where student_id=?`;

    query(sql, [req.body.studentId])
      .then(function (result) {
        console.log('删除成功：', result);

        res.json({
          code: '200',
          msg: '删除成功'
        });
      })
      .catch(function (err) {
        res.json({
          code: '1002',
          msg: '操作失败',
          err: err
        })
      })
  },
  add: function (req, res) {
    console.log('新增请求参数：', req.body);
    let studentName = req.body.studentName;
    let age = Number(req.body.age);
    let sex = Number(req.body.sex);
    let tel = req.body.tel;
    let classId = Number(req.body.classId);
    let nu = ''; // 学号
    let nation = req.body.nation;
    let address = req.body.address;

    const sql = `insert into students values(null,?,?,?,?,?,1,?,?,?,DEFAULT)`;

    console.log('转化后：', [studentName, age, sex, tel, classId, nu, nation, address]);

    query(sql, [studentName, age, sex, tel, classId, nu, nation, address])
      .then(function (result) {
        console.log('新增之后的结果：', result);

        res.json({
          code: '200',
          msg: '操作成功'
        });
      })
      .catch(function (err) {
        res.json({
          code: '1002',
          msg: '操作失败',
          err
        });
      });
  },
  edit: function (req, res) {
    console.log('编辑请求参数：', req.body);
    // let studentName = req.body.studentName;
    // let age = req.body.age;
    // let sex = req.body.sex;
    // let tel = req.body.tel;
    // let classId = req.body.classId;
    // let nation = req.body.nation;
    // let address = req.body.address;
    // let startSchool = req.body.startSchool;
    // let studentId = req.body.studentId;

    // ES6 解构
    let { studentName, age, sex, tel, classId, nation, address, startSchool, studentId } = req.body;

    const sql = `UPDATE students SET student_name=?,student_age=?,student_sex=?,student_tel=?,class_id=?,nation=?,address=?,start_school=? WHERE student_id=?`;

    query(sql, [studentName, age, sex, tel, classId, nation, address, startSchool, studentId])
      .then(function (result) {
        console.log('编辑之后的结果：', result);
        res.json({
          code: '200',
          msg: '操作成功'
        });
      })
      .catch(function (err) {
        res.json({
          code: '1002',
          msg: '操作失败',
          err
        });
      });
  },
  getById: function (req, res) {
    console.log('新增请求参数：', req.query);
    const studentId = req.query.studentId;

    const sql = `SELECT student_id studentId,student_name studentName,student_age age,student_sex sex,student_tel tel,class_id classId,student_nu studentNu,nation,address,start_school startSchool FROM students WHERE student_id=?`;

    query(sql, [studentId])
      .then(function (result) {
        console.log('查询某一个学生的结果：', result);

        res.json({
          code: '200',
          msg: '操作成功',
          data: result[0]
        });
      })
      .catch(function (err) {
        res.json({
          code: '1002',
          msg: '操作失败',
          err
        });
      });
  }
}